package com.ls.mq.util;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Dictionary;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class Toolkit {
	static Logger logger = Logger.getLogger(Toolkit.class);
	private static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
	public static boolean isEmpty(Object value) {
		if (value == null) {
			return true;
		}
		if ((value instanceof String) && (((String) value).trim().length() <= 0))
			return true;
		if ((value instanceof Object[]) && (((Object[]) value).length <= 0))
			return true;
		if ((value instanceof Collection) && ((Collection) value).size() <= 0)
			return true;
		if ((value instanceof Dictionary) && ((Dictionary) value).size() <= 0)
			return true;
		return false;
	}

	public static boolean notEmpty(Object obj) {
		return !isEmpty(obj);
	}

	/**
	 * Causes the currently executing thread to sleep (temporarily cease
	 * execution) for the specified number of milliseconds, subject to the
	 * precision and accuracy of system timers and schedulers. The thread does
	 * not lose ownership of any monitors.
	 * 
	 * Parameters: millis the length of time to sleep in milliseconds Throws:
	 * IllegalArgumentException - if the value of millis is negative
	 * InterruptedException - if any thread has interrupted the current thread.
	 * The interrupted status of the current thread is cleared when this
	 * exception is thrown.
	 * 
	 * @param millis
	 */
	public static void sleep(long millis) {
		try {
//			logger.info("开始睡眠 "+millis);
			Thread.sleep(millis);
//			logger.info("睡好了");
		} catch (InterruptedException e) {
			logger.info("睡眠中断",e);
		}
	}
	
	public static Gson getDefaultJson(){
		return gson;
	}
	
	public static String toJson(Object src) {
		return getDefaultJson().toJson(src);
	}

	public static <T> T fromJson(String json, Class<T> classOfT) {
		T t = getDefaultJson().fromJson(json, classOfT);
		return t;
	}
	
	
	public static <T> T fromJson(String json, Type type) {
		T t = getDefaultJson().fromJson(json, type);
		return t;
	}
}
